package plp

import (
	"github.com/Aiicy/htmlquery"
	"github.com/ruoklive/proxypool/internal/ipgetter/base"
	"github.com/ruoklive/proxypool/pkg/models"
	"github.com/ruoklive/proxypool/pkg/register"
	"go.uber.org/zap"
	"io"
)

func init() {
	register.Add(func() register.IPGetter {
		return New()
	})
}

type IPGetter struct {
	base.Base
}

func New() *IPGetter {
	g := &IPGetter{}
	g.Base = base.NewBase("https://list.proxylistplus.com/Fresh-HTTP-Proxy-List-1",g.Parse,"PLP")
	return g
}

//PLP get ip from proxylistplus.com
func (g *IPGetter) Parse(reader io.ReadCloser) (result []*models.IP) {
	doc, _ := htmlquery.Parse(reader)
	trNode, err := htmlquery.Find(doc, "//div[@class='hfeed site']//table[@class='bg']//tbody//tr")
	if err != nil {
		g.Error("Parse fail!",zap.Error(err))
		return
	}
	for i := 3; i < len(trNode); i++ {
		tdNode, _ := htmlquery.Find(trNode[i], "//td")
		ip := htmlquery.InnerText(tdNode[1])
		port := htmlquery.InnerText(tdNode[2])
		Type := htmlquery.InnerText(tdNode[6])

		IP := models.NewIP()
		IP.Data = ip + ":" + port

		if Type == "yes" {
			IP.Type1 = "http"
			IP.Type2 = "https"

		} else if Type == "no" {
			IP.Type1 = "http"
		}
		g.Info("GET IP", zap.String("ip",IP.String()))
		result = append(result, IP)
	}

	g.Info("done.")
	return
}
